package com.drojian.demo1.myself;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

public class FlowLayout extends ViewGroup {
    private int paddingLeft = 30;
    private int paddingTop = 20;

    private List<List<View>> allLines; //记录所有的行，用于layout
    List<Integer> lineHeights = new ArrayList<>(); //记录行高

    public FlowLayout(Context context) {
        super(context);
    }

    //用过反射解析xml
    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //解析自定义主题、自定义属性
    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initMeasureParams() {
        allLines = new ArrayList<>();
        lineHeights = new ArrayList<>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        initMeasureParams();
        //1.度量子view
        int childCount = getChildCount();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();


        List<View> lineViews = new ArrayList<>();//保存一行所有的view
        int lineWidthUsed = 0; //记录宽度用了多少
        int lineHeight = 0;//记录一行行高

        int selfWidth = MeasureSpec.getSize(widthMeasureSpec);//viewGroup解析的宽度
        int selfHeight = MeasureSpec.getSize(heightMeasureSpec);//viewGroup解析的高度

        int parentNeedWidth = 0;
        int parentNeedHeight = 0;

        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            //获取子view的layout的属性
            LayoutParams layoutParams = childView.getLayoutParams();
            //获取子view的宽高（1.父宽度  2.除去左右边距 3.子view的宽度）
            int childWidthMeasureSpec = getChildMeasureSpec(
                    widthMeasureSpec, paddingLeft + paddingRight, layoutParams.width);
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingBottom + paddingTop, layoutParams.height);
            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            int childMeasuredWidth = childView.getMeasuredWidth();
            int childMeasuredHeight = childView.getMeasuredHeight();
            //进行判断是否需要换行
            if (lineWidthUsed + childMeasuredWidth > selfWidth) {
                //childWidthMeasureSpec
                allLines.add(lineViews);
                lineHeights.add(lineHeight);
                //换行保留宽度和高度
                parentNeedHeight = parentNeedHeight + lineHeight;
                parentNeedWidth = Math.max(parentNeedWidth, lineWidthUsed);


                lineViews = new ArrayList<>();
                lineWidthUsed = 0;
                lineHeight = 0;
            }

            lineViews.add(childView);
            //每行都有自己的宽高
            lineWidthUsed = lineWidthUsed + childMeasuredWidth;
            lineHeight = Math.max(lineHeight, childMeasuredHeight);

            //判断是否为最后一行元素
            if (i == childCount - 1) {
                allLines.add(lineViews);
                lineHeights.add(lineHeight);

                parentNeedHeight = parentNeedHeight + lineHeight;
                parentNeedWidth = Math.max(parentNeedWidth, lineWidthUsed);
            }
        }
        //2.确定自己的view大小
        //判断view是否已经直接指定了大小
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int realWidth = (widthMode == MeasureSpec.EXACTLY) ? widthMode : parentNeedWidth;
        int realHeight = (heightMode == MeasureSpec.EXACTLY) ? heightMode : parentNeedHeight;
        setMeasuredDimension(realWidth, realHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int lineCount = allLines.size();
        for (int i = 0; i < lineCount; i++) {
            List<View> lineViews = allLines.get(i);
            int lineHeight = lineHeights.get(i);
            for (int j = 0; j < lineViews.size(); j++) {
                int left = paddingLeft;
                int top = paddingTop;
                int right = paddingLeft + lineViews.get(j).getMeasuredWidth();
                int bottom = paddingTop + lineViews.get(j).getMeasuredHeight();
                lineViews.get(j).layout(left, top, right, bottom);
                paddingLeft = right;
            }
            paddingLeft = getPaddingLeft();
            paddingTop = paddingTop + lineHeight;
        }
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
    }
}
